package greedy.总结.基础;

import java.util.Arrays;

public class leetcode1710卡车上的最大单元数 {
    public int maximumUnits(int[][] boxTypes, int truckSize) {
        Arrays.sort(boxTypes, (o1, o2) -> o2[1] - o1[1]);
        int sum = 0;
        int index = 0;
        while (truckSize > 0 && index < boxTypes.length) {
            int boxNumber = boxTypes[index][0];
            if (truckSize >= boxNumber) {
                truckSize -= boxNumber;
                sum += boxTypes[index][1] * boxNumber;
            } else {
                truckSize -= truckSize;
                sum += boxTypes[index][1] * truckSize;
            }
            index++;
        }
        return sum;
    }
}
